<template>
  <!--  <div class="login" :style="'background-image: url('+bgImgUrl+')'">-->
  <div class="login">
    <div class="single-switch-lng">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <a title="switch language"><i class="ti ti-language ti-md"></i></a>
        <el-dropdown-menu slot="dropdown" class="pop-item-wrap pop-item-wrap-lang">
          <el-dropdown-item @click.native="changeLng('zh')">
            <a :class="lngType == 'zh' ? 'active' : ''">中文</a>
          </el-dropdown-item>
          <el-dropdown-item @click.native="changeLng('en')">
            <a :class="lngType == 'en' ? 'active' : ''">English</a>
          </el-dropdown-item>
          <!-- <el-dropdown-item>
            <a>French</a>
          </el-dropdown-item>
          <el-dropdown-item>
            <a>Arabic</a>
          </el-dropdown-item>
          <el-dropdown-item>
            <a>German</a>
          </el-dropdown-item> -->
        </el-dropdown-menu>
      </el-dropdown>

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <a><i class="ti ti-md ti-sun"></i></a>
        <el-dropdown-menu slot="dropdown" class="pop-item-wrap pop-item-wrap-theme">
          <el-dropdown-item @click.native="changeStyle('light-style')">
            <a :class="!curStyle || curStyle == 'light-style' ? 'active' : ''"><i class="ti ti-sun me-2"></i>
              Light</a>
          </el-dropdown-item>
          <el-dropdown-item @click.native="changeStyle('dark-style')">
            <a :class="curStyle == 'dark-style' ? 'active' : ''"><i class="ti ti-moon me-2"></i> Dark</a>
          </el-dropdown-item>
          <!-- <el-dropdown-item>
            <a ><i class="ti ti-device-desktop me-2"></i> System</a>
          </el-dropdown-item> -->
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="wrap">
      <!-- <div class="div-a"></div> -->
      <el-card>
        <div class="login-title">
          <img v-if="logo" :src="logo"><span>{{ $t('login.title') }}</span>
        </div>
        <h4 class="mb-1 pt-2">{{$t('login.subtitle')}} 👋</h4>
        <p class="mb-4">{{$t('login.subtitdesc')}}</p>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username" :label="$t('login.username')">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" :placeholder="$t('plc.username')">
            </el-input>
          </el-form-item>
          <el-form-item prop="password" :label="$t('login.password')">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" :placeholder="$t('plc.password')" show-password
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="showValidCode">
            <div class="code-row">
              <el-input v-model="loginForm.code" auto-complete="off" :placeholder="$t('login.code')" style="width: 63%;float: left"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" />
            </div>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" class="remember-pwd">
            {{$t('login.remember')}}
          </el-checkbox>
          <el-form-item>
            <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin" class="login-btn">
              <span v-if="!loading">{{$t('login.login')}}</span>
              <span v-else>{{$t('login.loginloading')}}</span>
            </el-button>
            <div class="register-row">
              {{$t('login.new')}}<router-link class="link-type" :to="'/register'">{{$t('login.register')}}</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getCodeImg, getLoginInfo } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import logoImg from '@/assets/logo/logo.png'

export default {
  name: "Login",
  data() {
    return {
      lngType: '',
      curStyle:'',
      showValidCode: false,
      codeUrl: "",
      cookiePassword: "",
      logo: logoImg,
      bgImgUrl: null,
      websiteName: 'Rent Mail',
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "submit", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "submit", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "submit", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    let _theme = localStorage.getItem('v-style');
    this.curStyle = localStorage.getItem('v-style') || 'light-style';
    this.curTheme = _theme;
    if (_theme) {
      let _htmlEl = document.getElementsByTagName('html')[0];
      if (this.isCollapse) {
        _htmlEl.setAttribute('class', 'layout-navbar-fixed layout-compact layout-menu-collapsed ' + _theme + ' layout-menu-fixed');
      } else {
        _htmlEl.setAttribute('class', 'layout-navbar-fixed layout-compact ' + _theme + ' layout-menu-fixed');
      }
    }

    this.lngType = localStorage.getItem('sys-language') || 'en';
    let loginId = this.$route.query && this.$route.query.id;
    console.log('loginId:', loginId)
    if (loginId == null) {
      loginId = localStorage.getItem("w_loginId")
    }

    this.getCode();
    this.getCookie();
    this.initLoginInfo(loginId);

  },
  methods: {
    changeStyle(_style) {
      let _htmlEl = document.getElementsByTagName('html')[0];
      let _class = _htmlEl.getAttribute('class');
      let _classArray = ['layout-compact', 'layout-menu-fixed'];

      _classArray.push(_style);

      if (_class.indexOf('-collapsed') > -1) {
        _classArray.push('layout-menu-collapsed');
      }
      if (_class.indexOf('theme-') > -1) {
        _classArray.push(localStorage.getItem('v-theme'));
      }
      if (_class.indexOf('layout-navbar-') > -1) {
        _classArray.push(localStorage.getItem('v-navbarType'));
      }else{
        _classArray.push('layout-navbar-fixed');
      }
      _htmlEl.setAttribute('class', _classArray.join(' '));
      this.curStyle = _style;
      localStorage.setItem('v-style',_style);
    },
    changeLng(_lng) {
      localStorage.setItem('sys-language', _lng);
      this.$i18n.locale = _lng;
      this.lngType = _lng;
    },
    initLoginInfo(loginId) {
      getLoginInfo(loginId).then(res => {
        // console.log(res)
        if (res == null || res.data == null) {
          return;
        }
        this.logo = res.data.logoImgUrl
        this.bgImgUrl = res.data.loginImgUrl
        this.websiteName = res.data.name
        localStorage.setItem("w_loginId", loginId)
        localStorage.setItem("w_logo", this.logo)
        localStorage.setItem("w_logo_title", this.websiteName)
      });
    },
    getCode() {
      const uuid = Cookies.get("uuid");
      console.log(uuid)
      getCodeImg(uuid).then(res => {
        if (res != null && res.img != null)
          this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
        Cookies.set("uuid", res.uuid, { expires: 1 });
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      const cnt = Cookies.get("cnt");
      if (cnt !== undefined) {
        if (parseInt(cnt) >= 5) {
          this.showValidCode = true;
        }
      }
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          const username = this.loginForm.username
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              Cookies.remove("cnt");
              this.$router.push({ path: this.redirect || "/" });
              sessionStorage.setItem('w_username', username)
              this.socketApi.initWebSocket()
            })
            .catch((res) => {
              // console.log(res)
              this.loading = false;
              if ("needcode" === res) {
                this.showValidCode = true;
              }
              const cnt = Cookies.get("cnt");
              if (cnt !== undefined) {
                Cookies.set("cnt", (parseInt(cnt) + 1) + "");
              } else {
                Cookies.set("cnt", "1");
              }
              this.getCode();
            });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.div-a {
  width: 50%;
  width: 80%;
  filter: alpha(Opacity=60);
  -moz-opacity: 0.6;
  opacity: 0.6;
}

.login {
  // min-width: 360px;
  // display: table;
  // align-items: center;
  // text-align: center;
  // vertical-align: middle;
  // width: 100%;
  // height: 100%;
  // background-image: url("../assets/image/pagebg2.png");
  // background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 1.5rem;
  padding-top: 1.5rem;
  min-height: 100vh;
  width: 100%;

  .wrap {
    position: relative;
    max-width: 400px;
    width: 100%;
    padding-top: 1.5rem;
    padding-bottom: 1.5rem;

    &:before {
      width: 238px;
      height: 233px;
      content: " ";
      position: absolute;
      top: -55px;
      left: -40px;
      background-image: url('../assets/image/login_tl_logo.svg');
    }

    &:after {
      width: 180px;
      height: 180px;
      content: " ";
      position: absolute;
      z-index: -1;
      bottom: -30px;
      right: -56px;
      background-image: url('../assets/image/login_br_logo.svg');
    }

    .el-card {
      position: relative;
      // box-shadow: 0 0.25rem 1.125rem rgba(75, 70, 92, 0.1);
      box-shadow: 0 .25rem .875rem 0 rgba(38, 43, 67, .16);
      border-radius: 0.375rem;
      background-color: #fff;

      .login-btn {
        width: 100%;
        transition: .3s all linear;

        &:hover,
        &:focus {
          background: #685dd8 !important;
          border-color: #685dd8 !important;
        }
      }

      .el-form-item__label {
        margin-bottom: 0.25rem;
        font-size: 0.8125rem;
        color: #5d596c;
        text-transform: inherit;
        letter-spacing: inherit;
        font-weight: normal;
        line-height: 15px;

        &:before {
          content: ''
        }
      }

      .el-form-item {
        margin-bottom: 1rem;

        .el-input__inner {
          // height:1.5rem;
          line-height: 1.5rem;
          border-radius: 0.375rem;
          transition: .2s all linear;
          padding-top: 0.422rem;
          padding-bottom: 0.422rem;
          font-size: 0.9375rem;
          height: inherit;

          &:focus {
            color: #6f6b7d;
            // background-color: #fff;
            border-color: #7367f0;
            outline: 0;
            box-shadow: 0 0.125rem 0.25rem rgba(165, 163, 174, 0.3);
          }
        }
      }

      .remember-pwd {
        margin-bottom: 1rem;

        .el-checkbox__inner {
          width: 18px;
          height: 18px;
          border-radius: 0.375rem;
          border-color: #676b7b;
          border-width:2px;
          margin-top:-0.05rem;
          &:after {
            left: 5.75px;
            top: 2px;
          }
        }
        .el-checkbox__label {
          color: #6f6b7d;
          font-weight: 500;
        }
      }

      .register-row {
        text-align: center;
        color: #6f6b7d;
        margin-top: 1rem;

        a {
          color: #7367f0;

          &:hover {
            color: #8176f2;
          }
        }
      }

      .el-form-item__error {
        width: 100%;
        margin-top: 0.25rem;
        font-size: 0.8125rem;
        color: #ea5455;
        position: relative;
      }

      .el-card__body {
        padding: 2rem;

        h4 {
          margin-bottom: 0.25rem !important;
          padding-top: 0.5rem !important;
          margin-top: 0px;
          color: #5d596c;
          font-size: 1.375rem;
          font-weight: 500;
          line-height: 1.37;
        }

        .mb-4 {
          margin-bottom: 1.5rem !important;
          margin-top: 0px;
          color: #6f6b7d;
        }
      }
    }
  }
}

.login-title {
  display: flex;
  align-items: center;
  margin-bottom: .5rem;
  justify-content: center;

  img {
    width: 50px;
  }

  span {
    border-width: 0px;
    // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
    font-weight: 600;
    font-style: normal;
    font-size: 22px;
    color: #6f6b7d;
    text-align: center;
    margin-left: 0.625rem;
  }

  p {
    font-weight: 400;
    font-style: normal;
    font-size: 18px;
    color: #FFFFFF;
    text-align: center;
    line-height: 40px;
  }
}

.login-div {
  height: 200px;
  width: 304px;
  line-height: 50px;
  text-align: center;
  display: table;
}

.login-form {
  border-radius: 6px;
  width: 380px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  // font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;

  span {
    // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
    font-weight: 400;
    font-style: normal;
    font-size: 10px;
    color: #FFFFFF;
    text-align: center;
    line-height: 40px;
  }
}

@media (max-width:1280px) {
  .title {
    span {
      border-width: 0px;
      // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
      font-weight: 600;
      font-style: normal;
      font-size: 24px;
      color: #FFFFFF;
      text-align: center;
    }

    p {
      font-weight: 400;
      font-style: normal;
      font-size: 18px;
      color: #FFFFFF;
      text-align: center;
      line-height: 28px;
    }
  }
}

@media (max-width:680px) {
  .title {
    span {
      border-width: 0px;
      // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
      font-weight: 600;
      font-style: normal;
      font-size: 18px;
      color: #FFFFFF;
      text-align: center;
    }

    p {
      font-weight: 400;
      font-style: normal;
      font-size: 10px;
      color: #FFFFFF;
      text-align: center;
      line-height: 20px;
    }
  }

  @media (max-width:480px) {
    .title {
      span {
        border-width: 0px;
        // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
        font-weight: 600;
        font-style: normal;
        font-size: 18px;
        color: #FFFFFF;
        text-align: center;
      }

      p {
        font-weight: 400;
        font-style: normal;
        font-size: 10px;
        color: #FFFFFF;
        text-align: center;
        line-height: 20px;
      }
    }

    .col-left {
      width: 0px;
    }

    .col-center {
      padding: 0 25px 0px;
      width: 100%;
    }
  }
}
  .code-row{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>
